package com.passioneffect.widgets {
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;	

	/**
	 * @author Passion Effect
	 */
	public class OverHandlerObj extends EventDispatcher {
		private var target : DisplayObject;
		private var outHandler : Function;
		private var overHandler : Function;
		private var at : DisplayObjectContainer;
		private var rect : Rectangle;
		private var overed : Boolean = false;

		public function OverHandlerObj(t : DisplayObject,rect : Rectangle,at : DisplayObjectContainer,overHandler : Function,outHandler : Function) {
			target = t;
			this.overHandler = overHandler;
			this.outHandler = outHandler;
			this.rect = rect;
			this.at = at;
			target.stage.addEventListener(Event.ENTER_FRAME, over);
			target.stage.addEventListener(Event.ENTER_FRAME, enterHandler);
		}

		private function enterHandler(evt : Event) : void {
			if(!overed)return;
			if(!rect.contains(at.mouseX, at.mouseY)) {
				out();
			}
		}
		
		public function out() : void {
			overed = false;
			if(outHandler != null)outHandler(target);
		}

		private function over(event : Event) : void {
			if(overed)return;
			if(target.getRect(at).contains(at.mouseX, at.mouseY)) {
				overed = true;
				if(overHandler != null){
					overHandler(target);
				}
			}
		}
	}
}
